//
//  AppDelegate.m
//  YellowBridge
//
//  Created by 王右 on 2018/12/1.
//  Copyright © 2018 王右. All rights reserved.
//

#import "AppDelegate.h"
#import "LoginViewController.h"
#import "HomePageViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [_window makeKeyAndVisible];
//    if ([[CCCache defaultManager] objectForKey:Login_User_Info_Model]) {
//        [UIApplication sharedApplication].keyWindow.rootViewController = [[BaseNavigationViewController alloc] initWithRootViewController:[HomePageViewController new]];
//    }else{
//        _window.rootViewController = [[BaseNavigationViewController alloc] initWithRootViewController:[LoginViewController new]];
//    }
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    [self retryLogin];
    return YES;
}

- (void)logout{
    [[CCCache defaultManager] removeObjectForKey:Login_User_Info_Model];
    _window.rootViewController = [[BaseNavigationViewController alloc] initWithRootViewController:[LoginViewController new]];
    [self.window makeToast:@"账号登录信息失效,请重新登录!"];
}

- (void)retryLogin{
    
    NSString *account = User_Account;
    NSString *password = User_Password;
    if (!account || !password) {
        _window.rootViewController = [[BaseNavigationViewController alloc] initWithRootViewController:[LoginViewController new]];
        return;
    }
    UIViewController *vc = [UIViewController new];
    vc.view.backgroundColor = [UIColor whiteColor];
    _window.rootViewController = vc;
    [RequestManager postRequestWithModule:@"v1/login" param:@{@"account":account,@"password":password} model:nil success:^(NSDictionary *obj) {
        NSDictionary *user = obj[@"result"];
        LoginUserModel *model = [LoginUserModel objectWithDictionary:user];
        //        model.account = @"13402627262";
        model.account = account;
        model.password = password;
        [[NSUserDefaults standardUserDefaults] setValue:account forKey:@"LoginUserAccountCache"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [[CCCache defaultManager] setObject:model forKey:Login_User_Info_Model];
        [UIApplication sharedApplication].keyWindow.rootViewController = [[BaseNavigationViewController alloc] initWithRootViewController:[HomePageViewController new]];
    } failure:^(NSString *error) {
        self->_window.rootViewController = [[BaseNavigationViewController alloc] initWithRootViewController:[LoginViewController new]];
    }];
}

- (void)doLogin{
    NSString *account = User_Account;
    NSString *password = User_Password;
    if (account==nil || password==nil) {
        return;
    }
    [RequestManager postRequestWithModule:@"v1/login" param:@{@"account":account,@"password":password} model:nil success:^(NSDictionary *obj) {
        NSDictionary *user = obj[@"result"];
        LoginUserModel *model = [LoginUserModel objectWithDictionary:user];
        //        model.account = @"13402627262";
        model.account = account;
        model.password = password;
        [[NSUserDefaults standardUserDefaults] setValue:account forKey:@"LoginUserAccountCache"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [[CCCache defaultManager] setObject:model forKey:Login_User_Info_Model];
    } failure:nil];
}

- (void)applicationWillResignActive:(UIApplication *)application {

}


- (void)applicationDidEnterBackground:(UIApplication *)application {

}


- (void)applicationWillEnterForeground:(UIApplication *)application {
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    [self doLogin];
}


- (void)applicationWillTerminate:(UIApplication *)application {
}


@end
